import random

# 常量定义
AREA_CODES = ['110000', '310000', '440000']
ID_AREA_CODES = ['110101', '310104', '440106', '510104']

# 号码生成器类
class NumberGenerator:
    @staticmethod
    def generate_credit_code():
        """生成一个符合规则的统一社会信用代码"""
        # 第一部分：登记管理部门代码(1位) + 机构类别代码(1位)
        part1 = random.choice(['11', '12', '13', '19'])  # 示例代码

        # 第二部分：登记管理机关行政区划码(6位)
        part2 = str(random.randint(110000, 659900))[:6]

        # 第三部分：主体标识码(9位)
        part3 = ''.join([str(random.randint(0, 9)) for _ in range(9)])

        # 第四部分：校验码(1位)
        code = part1 + part2 + part3
        weights = [3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]
        total = sum(int(c) * w for c, w in zip(code, weights))
        check_code = str((31 - total % 31) % 31)
        check_code = check_code.replace('10', 'A').replace('11', 'B').replace('12', 'C') \
            .replace('13', 'D').replace('14', 'E').replace('15', 'F') \
            .replace('16', 'G').replace('17', 'H').replace('18', 'J') \
            .replace('19', 'K').replace('20', 'L').replace('21', 'M') \
            .replace('22', 'N').replace('23', 'P').replace('24', 'Q') \
            .replace('25', 'R').replace('26', 'T').replace('27', 'U') \
            .replace('28', 'W').replace('29', 'X').replace('30', 'Y')

        return code + check_code

    @staticmethod
    def generate_multiple_codes(count):
        """生成多个统一社会信用代码"""
        return [NumberGenerator.generate_credit_code() for _ in range(count)]

    @staticmethod
    def generate_zheng_code():
        """生成一个符合规则的中征码"""
        # 前6位行政区划代码(示例)
        area_code = random.choice(['110000', '310000', '440000'])
        # 中间8位顺序号
        serial = ''.join([str(random.randint(0, 9)) for _ in range(8)])
        base = area_code + serial
        # 计算校验码(ISO 7064 MOD 97-10算法)
        remainder = int(base) % 97
        check_code = f"{98 - remainder:02d}"
        return base + check_code

    @staticmethod
    def generate_multiple_zheng(count):
        """生成多个中征码"""
        return [NumberGenerator.generate_zheng_code() for _ in range(count)]

    @staticmethod
    def generate_id_card():
        """生成一个符合规则的身份证号码"""
        # 前6位 - 地区码
        area_codes = ['110101', '310104', '440106', '510104']  # 示例地区码
        area_code = random.choice(area_codes)

        # 中间8位 - 出生日期
        year = random.randint(1950, 2000)
        month = random.randint(1, 12)
        day = random.randint(1, 28)  # 简化处理
        birth_date = f"{year}{month:02d}{day:02d}"

        # 顺序码3位
        seq_code = f"{random.randint(0, 999):03d}"

        # 前17位
        first_17 = area_code + birth_date + seq_code

        # 计算校验码
        weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        check_map = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
        total = sum(int(c) * w for c, w in zip(first_17, weights))
        check_code = check_map[total % 11]

        return first_17 + check_code

    @staticmethod
    def generate_multiple_ids(count):
        """生成多个身份证号码"""
        return [NumberGenerator.generate_id_card() for _ in range(count)]

    @staticmethod
    def generate_business_license():
        """生成一个符合规则的企业营业执照号码"""
        # 前6位 - 行政区划代码
        area_code = random.choice(['110000', '310000', '440000'])
        # 中间8位 - 顺序号
        sequence = ''.join([str(random.randint(0, 9)) for _ in range(8)])
        # 第15位 - 校验码
        base = area_code + sequence
        weights = [3, 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10]
        total = sum(int(c) * w for c, w in zip(base, weights))
        check_code = str((11 - total % 11) % 11).replace('10', 'X')
        return base + check_code

    @staticmethod
    def generate_multiple_licenses(count):
        """生成多个营业执照号码"""
        return [NumberGenerator.generate_business_license() for _ in range(count)]

    @staticmethod
    def generate_org_code():
        """生成一个符合规则的组织机构代码"""
        # 前7位 - 本体代码
        body_code = ''.join([str(random.randint(0, 9)) for _ in range(7)])

        # 第8位 - 校验码
        weights = [3, 7, 9, 10, 5, 8, 4, 2]
        total = sum(int(c) * w for c, w in zip(body_code, weights))
        check_code = str(11 - total % 11)
        check_code = check_code.replace('10', 'X').replace('11', '0')

        return body_code + '-' + check_code

    @staticmethod
    def generate_multiple_orgs(count):
        """生成多个组织机构代码"""
        return [NumberGenerator.generate_org_code() for _ in range(count)]
